﻿using SmartSpace.Vote;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartSpace.Vote
{
    public class VoteServices : IThemeService
    {
        public ServiceResult SaveVoteTheme(VoteTheme vote)
        {
            using (VoteDbContext context = new VoteDbContext())
            {
                if (context.Set<VoteTheme>().Where(p => p.Id == vote.Id).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    //exist not in db --> add
                    context.Set<VoteTheme>().Add(vote);
                }
                else
                {
                    context.Entry(vote).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "Save Vote Success");
                }
                else
                {
                    Logger.Debug("vote", String.Format("Save vote {0},{1} failure", vote.Id, vote.Name));
                    return new ServiceResult(false, "Save vote Failure");
                }
            }
        }

        public ServiceResult DeleteVoteTheme(Guid id)
        {
            using (VoteDbContext context = new VoteDbContext())
            {
                SingleResult<VoteTheme> result = GetVoteTheme(id);

                if (result.Data == null)
                {
                    Logger.Debug("vote", String.Format("Delete vote {0} failure", id));
                    return new ServiceResult(false, "Delete VoteTheme Failure");
                }
                else
                {
                    context.Entry(result.Data).State = System.Data.Entity.EntityState.Deleted;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "Delete VoteTheme Success");
                }
                else
                {
                    return new ServiceResult(false, "Delete VoteTheme Failure");
                }
            }
        }

        public DataTable GetAllThemes()
        {
            String sql = "select * from vote_votetheme";
            DataTable table = Common.GetDataTableNoParm(sql);
            try
            {
                return table;
            }
            catch
            {
                Logger.Debug("Folder", "Get All votetheme Error!");
                return null;
            }
        }

        public SingleResult<VoteTheme> GetVoteTheme(Guid id)
        {
            using (VoteDbContext context = new VoteDbContext())
            {
                VoteTheme vote = new VoteTheme();
                vote = context.Set<VoteTheme>().Where(p => p.Id == id).SingleOrDefault();
                if (vote == null)
                {
                    return new SingleResult<VoteTheme>(false, "Get votetheme failure", null);
                }
                else
                {
                    return new SingleResult<VoteTheme>(vote);
                }
            }
        }
    }
}
